package com.monkey.demolist.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.monkey.demolist.R;

/**
* Description: HSLA色彩模式对图像的处理
* Author: Arthor
* Date: 2017/10/19
* Time: 10:43
*/

public class ColorMatrixActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private static int MAX_VALUE = 255;
    private static int MID_VALUE = 127;
    private float mHue, mSaturation, mLum;
    ImageView imageView;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_matrix);

        imageView = (ImageView) findViewById(R.id.image);
        bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.aa);
        imageView.setImageBitmap(bitmap);

        SeekBar hueSb = (SeekBar) findViewById(R.id.hue);
        SeekBar saturationSb = (SeekBar) findViewById(R.id.saturation);
        SeekBar lumSb = (SeekBar) findViewById(R.id.lum);

        hueSb.setOnSeekBarChangeListener(this);
        hueSb.setMax(MAX_VALUE);
        hueSb.setProgress(MID_VALUE);

        saturationSb.setOnSeekBarChangeListener(this);
        saturationSb.setMax(MAX_VALUE);
        saturationSb.setProgress(MID_VALUE);

        lumSb.setOnSeekBarChangeListener(this);
        lumSb.setMax(MAX_VALUE);
        lumSb.setProgress(MID_VALUE);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.hue:
                mHue = (progress - MID_VALUE) * 1.0f / MID_VALUE * 180;
                break;
            case R.id.saturation:
                mSaturation = progress * 1.0f / MID_VALUE;
                break;
            case R.id.lum:
                mLum = progress * 1.0f / MID_VALUE;
                break;
        }
        Bitmap bmp = handleImageEffect(bitmap,mHue,mSaturation,mLum);
        imageView.setImageBitmap(bmp);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    /**
     * 通过HSLA颜色模式改变图片效果
     *
     * @param bitmap 要处理的图片
     * @param hue 色相
     * @param saturation 饱和度
     * @param lum 亮度
     * @return 返回处理后的图片
     */
    public Bitmap handleImageEffect(Bitmap bitmap, float hue, float saturation, float lum) {

        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        //修改色相
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0,hue);//0 表示修改red
        hueMatrix.setRotate(1,hue);//1 表示修改green
        hueMatrix.setRotate(2,hue);//2 表示修改blue

        //修改饱和度
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        //修改亮度
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum,lum,lum,1);

        //融合色相 饱和度 亮度
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bitmap,0,0,paint);
        return bmp;
    }
}
